package com.syb.dao.impl;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.syb.dao.UserDao;
import com.syb.entity.user.SysUser;
import com.syb.util.BaseDao;
import com.syb.util.PageResults;

@Repository
public class UserDaoImpl extends BaseDao implements UserDao {

	@Override
	public List<SysUser> findUserList(PageResults<SysUser> page)throws Exception {

		List<Object> paramList = new ArrayList();
		List<SysUser> userList = new ArrayList();
		StringBuffer sub = new StringBuffer();
		sub.append(" from SysUser where 1=1 ");
		if(page.getParamMap().get("name")!=null && page.getParamMap().get("name").length()>0){
			sub.append(" and sys_name like ? ");
			paramList.add("%"+page.getParamMap().get("name")+"%");
		}
		try{
			Query query = getSessionFactory().getCurrentSession().createQuery(sub.toString());
			for(int i=0;i<paramList.size();i++){
				query.setParameter(i, paramList.get(i));
			}
			query.setFirstResult(page.getCurrentPage());
			query.setMaxResults(page.getPageSize());
			userList = query.list();
		}catch(Exception e){
			throw new Exception(e.getMessage());
		}
		return userList;
	}

	@Override
	public int findUsercount(PageResults<SysUser> page) throws Exception {
		List<BigInteger> sysUserCount = new ArrayList<BigInteger>();
		List<Object> paramList = new ArrayList();
		StringBuffer sub = new StringBuffer();
		sub.append("select count(*) from sys_user where 1=1 ");
		if(page.getParamMap().get("name")!=null && page.getParamMap().get("name").length()>0){
			sub.append(" and sys_name like ? ");
			paramList.add("%"+page.getParamMap().get("name")+"%");
		}
		try{
			Query query = getSessionFactory().getCurrentSession().createSQLQuery(sub.toString());
			for(int i=0;i<paramList.size();i++){
				query.setParameter(i, paramList.get(i));
			}
			sysUserCount = query.list();
		}catch(Exception e){
			throw new Exception(e.getMessage());
		}
		return sysUserCount.get(0).intValue();
	}

	@Override
	public SysUser findUserById(Integer id) throws Exception {
		SysUser sysUser = null;
		try{
			sysUser = (SysUser) this.getSessionFactory().getCurrentSession().get(SysUser.class, id);
		}catch(Exception e){
			throw new Exception(e.getMessage());
		}
		return sysUser;
	}

	@Override
	public SysUser findUserByName(String name) throws Exception {
		SysUser sysUser = null;
		List<SysUser> list= new ArrayList<SysUser>();
		try{
			list = this.getSessionFactory().getCurrentSession().createQuery("  from  SysUser where sys_name = ?").setParameter(0, name).list();
		}catch(Exception e){
			throw new Exception(e.getMessage());
		}
		if(list.size()>0){
			sysUser = list.get(0);
		}
		return sysUser;
	}

	@Override
	public void save(SysUser sysUser) throws Exception {
		try{
			this.getSessionFactory().getCurrentSession().saveOrUpdate(sysUser);
		}catch(Exception e){
			throw new Exception(e.getMessage());
		}
	}

	@Override
	public void delete(SysUser sysUser) throws Exception {
		try{
			this.getSessionFactory().getCurrentSession().delete(sysUser);
		}catch(Exception e){
			throw new Exception(e.getMessage());
		}
	}

}
